{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from warnings import filterwarnings\n",
    "filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.读取数据 5分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"车贷违约.csv\",engine=\"python\",encoding='gbk')\n",
    "#还款能力  收入，收入借贷比, 职业，月收入方差/标准差，存款 ，存款借贷比\n",
    "#还款意愿(信用) 芝麻信用，逾期次数，信用卡数量，信用卡额度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(r'车贷违约.csv',encoding='gbk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>申请者Id</th>\n",
       "      <th>账户号</th>\n",
       "      <th>是否违约Y</th>\n",
       "      <th>汽车购买时间</th>\n",
       "      <th>汽车制造商</th>\n",
       "      <th>是否曾经破产</th>\n",
       "      <th>五年内信用不良事件数量</th>\n",
       "      <th>账户数量</th>\n",
       "      <th>最久账户存续时间(月)</th>\n",
       "      <th>在使用账户数量</th>\n",
       "      <th>...</th>\n",
       "      <th>汽车购买金额(元)</th>\n",
       "      <th>建议售价</th>\n",
       "      <th>分期付款的首次交款</th>\n",
       "      <th>贷款期限(月)</th>\n",
       "      <th>贷款金额</th>\n",
       "      <th>贷款金额/建议售价*100</th>\n",
       "      <th>月均收入(元)</th>\n",
       "      <th>行使历程(Mile)</th>\n",
       "      <th>是否二手车</th>\n",
       "      <th>样本权重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2314049</td>\n",
       "      <td>11613</td>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>FORD</td>\n",
       "      <td>N</td>\n",
       "      <td>7.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>17200.00</td>\n",
       "      <td>17350.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>36</td>\n",
       "      <td>17200.00</td>\n",
       "      <td>99.0</td>\n",
       "      <td>6550.00</td>\n",
       "      <td>24000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>63539</td>\n",
       "      <td>13449</td>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>DAEWOO</td>\n",
       "      <td>N</td>\n",
       "      <td>0.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>240.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>...</td>\n",
       "      <td>19588.54</td>\n",
       "      <td>19788.0</td>\n",
       "      <td>683.54</td>\n",
       "      <td>60</td>\n",
       "      <td>19588.54</td>\n",
       "      <td>99.0</td>\n",
       "      <td>4666.67</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7328510</td>\n",
       "      <td>14323</td>\n",
       "      <td>1</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>PLYMOUTH</td>\n",
       "      <td>N</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>13595.00</td>\n",
       "      <td>11450.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>60</td>\n",
       "      <td>10500.00</td>\n",
       "      <td>92.0</td>\n",
       "      <td>2000.00</td>\n",
       "      <td>19600.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>8725187</td>\n",
       "      <td>15359</td>\n",
       "      <td>1</td>\n",
       "      <td>1997.0</td>\n",
       "      <td>FORD</td>\n",
       "      <td>N</td>\n",
       "      <td>3.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>12999.00</td>\n",
       "      <td>12100.0</td>\n",
       "      <td>3099.00</td>\n",
       "      <td>60</td>\n",
       "      <td>10800.00</td>\n",
       "      <td>118.0</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4275127</td>\n",
       "      <td>15812</td>\n",
       "      <td>0</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>TOYOTA</td>\n",
       "      <td>N</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>104.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>26328.04</td>\n",
       "      <td>22024.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>60</td>\n",
       "      <td>26328.04</td>\n",
       "      <td>122.0</td>\n",
       "      <td>4144.00</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0</td>\n",
       "      <td>4.75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     申请者Id    账户号  是否违约Y  汽车购买时间     汽车制造商 是否曾经破产  五年内信用不良事件数量  账户数量  \\\n",
       "0  2314049  11613      1  1998.0      FORD      N          7.0   9.0   \n",
       "1    63539  13449      0  2000.0    DAEWOO      N          0.0  21.0   \n",
       "2  7328510  14323      1  1998.0  PLYMOUTH      N          7.0  10.0   \n",
       "3  8725187  15359      1  1997.0      FORD      N          3.0  10.0   \n",
       "4  4275127  15812      0  2000.0    TOYOTA      N          0.0  10.0   \n",
       "\n",
       "   最久账户存续时间(月)  在使用账户数量  ...  汽车购买金额(元)     建议售价  分期付款的首次交款  贷款期限(月)  \\\n",
       "0         64.0      2.0  ...   17200.00  17350.0       0.00       36   \n",
       "1        240.0     11.0  ...   19588.54  19788.0     683.54       60   \n",
       "2         60.0      NaN  ...   13595.00  11450.0       0.00       60   \n",
       "3         35.0      5.0  ...   12999.00  12100.0    3099.00       60   \n",
       "4        104.0      2.0  ...   26328.04  22024.0       0.00       60   \n",
       "\n",
       "       贷款金额  贷款金额/建议售价*100  月均收入(元)  行使历程(Mile)  是否二手车  样本权重  \n",
       "0  17200.00           99.0  6550.00     24000.0      1  1.00  \n",
       "1  19588.54           99.0  4666.67        22.0      0  4.75  \n",
       "2  10500.00           92.0  2000.00     19600.0      1  1.00  \n",
       "3  10800.00          118.0  1500.00     10000.0      1  1.00  \n",
       "4  26328.04          122.0  4144.00        14.0      0  4.75  \n",
       "\n",
       "[5 rows x 25 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5845 entries, 0 to 5844\n",
      "Data columns (total 25 columns):\n",
      " #   Column                 Non-Null Count  Dtype  \n",
      "---  ------                 --------------  -----  \n",
      " 0   申请者Id                  5845 non-null   int64  \n",
      " 1   账户号                    5845 non-null   int64  \n",
      " 2   是否违约Y                  5845 non-null   int64  \n",
      " 3   汽车购买时间                 5844 non-null   float64\n",
      " 4   汽车制造商                  5546 non-null   object \n",
      " 5   是否曾经破产                 5628 non-null   object \n",
      " 6   五年内信用不良事件数量            5632 non-null   float64\n",
      " 7   账户数量                   5632 non-null   float64\n",
      " 8   最久账户存续时间(月)            5629 non-null   float64\n",
      " 9   在使用账户数量                4426 non-null   float64\n",
      " 10  在使用可循环贷款账户数量(比如信用卡)    5207 non-null   float64\n",
      " 11  在使用可循环贷款帐户余额(比如信用卡欠款)  5367 non-null   float64\n",
      " 12  可循环贷款帐户限额(信用卡授权额度)     5367 non-null   float64\n",
      " 13  可循环贷款帐户使用比例(余额/限额)     5845 non-null   int64  \n",
      " 14  FICO打分                 5531 non-null   float64\n",
      " 15  汽车购买金额(元)              5845 non-null   float64\n",
      " 16  建议售价                   5844 non-null   float64\n",
      " 17  分期付款的首次交款              5845 non-null   float64\n",
      " 18  贷款期限(月)                5845 non-null   int64  \n",
      " 19  贷款金额                   5845 non-null   float64\n",
      " 20  贷款金额/建议售价*100          5844 non-null   float64\n",
      " 21  月均收入(元)                5840 non-null   float64\n",
      " 22  行使历程(Mile)             5844 non-null   float64\n",
      " 23  是否二手车                  5845 non-null   int64  \n",
      " 24  样本权重                   5845 non-null   float64\n",
      "dtypes: float64(17), int64(6), object(2)\n",
      "memory usage: 1.1+ MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_val = data.isnull().sum().values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "da = data_val.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "da"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "#去掉前两列\n",
    "data.drop([\"申请者Id\",\"账户号\"],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4648\n",
       "1    1197\n",
       "Name: 是否违约Y, dtype: int64"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#因变量处理\n",
    "data.是否违约Y.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = data.loc[~((data.是否违约Y ==\"bad_ind\") | (data.是否违约Y.isna())) ,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异常值处理 +  自变量选择 10分 \n",
    "有用的变量才需要处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "#自变量选择： 1.业务上\n",
    "#             2.技术上（相关性（x,y都是数值，线性关系)，散点图（x,y都是数值），卡方检验 T检验 F检验）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#异常值处理 1.业务上，2 3σ 3.箱线图 ,   数据变换log,开平方， 倒数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(37.4400457088725,\n",
       " 0.006987182756537592,\n",
       " 19,\n",
       " array([[2.36962012e+02, 7.95174538e-01, 7.95174538e-01, 7.95174538e-01,\n",
       "         1.59034908e+00, 7.95174538e-01, 2.38552361e+00, 9.54209446e+00,\n",
       "         1.51083162e+01, 2.54455852e+01, 6.28187885e+01, 1.35179671e+02,\n",
       "         2.16287474e+02, 3.61009240e+02, 5.66959446e+02, 5.19248973e+02,\n",
       "         8.30957392e+02, 1.65634856e+03, 7.95174538e-01, 3.18069815e+00],\n",
       "        [6.10379877e+01, 2.04825462e-01, 2.04825462e-01, 2.04825462e-01,\n",
       "         4.09650924e-01, 2.04825462e-01, 6.14476386e-01, 2.45790554e+00,\n",
       "         3.89168378e+00, 6.55441478e+00, 1.61812115e+01, 3.48203285e+01,\n",
       "         5.57125257e+01, 9.29907598e+01, 1.46040554e+02, 1.33751027e+02,\n",
       "         2.14042608e+02, 4.26651437e+02, 2.04825462e-01, 8.19301848e-01]]))"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from  scipy  import stats\n",
    "data.汽车购买时间.value_counts()\n",
    "crosstable = pd.crosstab(data.是否违约Y,data.汽车购买时间)\n",
    "stats.chi2_contingency(crosstable)  \n",
    "#卡方检验的 H0： p1 =p2 = p3 =p4 = P, 分类型自变量对分类型因变量没有影响\n",
    "#alpha = 0.05   p=0.006  拒绝H0 原假设  \n",
    "#结论呢： "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把购买时间改为购买时长\n",
    "data.汽车购买时间[(data.汽车购买时间==\"0\") | (data.汽车购买时间.isna())] = \"9999\"\n",
    "data.汽车购买时间 = data.汽车购买时间.astype(int)\n",
    "data.汽车购买时间 = 2019 -data.汽车购买时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data.汽车购买时间[(data.汽车购买时间<0) | (data.汽车购买时间>100)] = data.汽车购买时间[data.汽车购买时间>0].median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-29-91f6525d3517>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-29-91f6525d3517>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    H0 ： μ1= μ2\u001b[0m\n\u001b[1;37m       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "stats.ttest_ind(data.汽车购买时间[data.是否违约Y==1],data.汽车购买时间[data.是否违约Y==0])\n",
    "H0 ： μ1= μ2\n",
    "alpha = 0.05   p=0.876  没有理由拒绝H0  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shangjie = 22.0 + 1.5*( 22.0-19.0)\n",
    "#方法1， 去掉异常\n",
    "data = data.loc[data.汽车购买时间 <shangjie,:]\n",
    "#方法2，盖帽法处理\n",
    "data.汽车购买时间[data.汽车购买时间>shangjie] = shangjie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.汽车制造商.describe(include=\"object\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#x 分类的，y分类的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "top38 = data.汽车制造商.value_counts()[data.汽车制造商.value_counts()>20].index\n",
    "data.汽车制造商[~(data.汽车制造商.isin(top38))] = \"其他\"\n",
    "crosstable = pd.crosstab(data.是否违约Y,data.汽车制造商)\n",
    "stats.chi2_contingency(crosstable)    \n",
    "#汽车制造商 对是否违约无显著影响\n",
    "#卡方检验原假设\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.drop([\"汽车制造商\"],inplace=True,axis =1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data.是否曾经破产[data.是否曾经破产.isna()] = \"未填写\"\n",
    "data.是否曾经破产.value_counts()\n",
    "crosstable = pd.crosstab(data.是否违约Y,data.是否曾经破产)\n",
    "stats.chi2_contingency(crosstable) \n",
    "# 是否曾经破产对Y 有显著影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_selection import SelectPercentile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "select = SelectPercentile(percentile=70) \n",
    "select.fit(data.iloc[:,4:].dropna(axis=1),data.是否违约Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "select.get_support()   #返回被选中特征和没被选中特征的掩码或者整数索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.iloc[:,4:].dropna(axis=1).columns[select.get_support()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_selection import f_classif 方差, f_regression 相关系数, chi2 卡方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data.五年内信用不良事件数量[data.五年内信用不良事件数量.isna()] =0\n",
    "data.五年内信用不良事件数量 = data.五年内信用不良事件数量.astype(int)\n",
    "stats.ttest_ind(data.五年内信用不良事件数量[data.是否违约Y==1],data.五年内信用不良事件数量[data.是否违约Y==0])\n",
    "#有显著影响\n",
    "#H0: μ1 = μ2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#stats.f_oneway(data.五年内信用不良事件数量[data.是否违约Y==1],data.五年内信用不良事件数量[data.是否违约Y==0])\n",
    "#方差分析的作用\n",
    "#1. 2个及两个以上总体均值检验\n",
    "#2. 方差齐性检验\n",
    "#3. 回归中检验 方程有效性  H0 w1=w2=w3=0;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.五年内信用不良事件数量.plot(kind=\"box\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sum(data.账户数量.isna())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s =data.账户数量[~((data.账户数量.isna())|(data.账户数量.isnull()))]\n",
    "s.astype(int).plot(kind=\"box\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.账户数量[((data.账户数量.isna())|(data.账户数量.isnull()))] = data.账户数量[~((data.账户数量.isna())|(data.账户数量.isnull()))].median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.账户数量 =np.log10(data.账户数量.astype(int))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.账户数量.plot(kind=\"box\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#相关系数\n",
    "data.rename({\"在使用可循环贷款账户数量(比如信用卡)\":\"在使用可循环贷款账户数量\"},axis=1,inplace=True)\n",
    "data_cor = data.loc[:,[\"账户数量\",\"在使用账户数量\",\"在使用可循环贷款账户数量\"]]\n",
    "data_cor.dropna(axis=0,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_cor.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data_cor.info()\n",
    "data_cor.账户数量  = data_cor.账户数量 .astype(int)\n",
    "data_cor.在使用账户数量  = data_cor.在使用账户数量 .astype(int)\n",
    "data_cor.loc[:,\"在使用可循环贷款账户数量\"]  =data_cor.loc[:,\"在使用可循环贷款账户数量\"]  .astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "cor_mat = data_cor.corr(method='spearman')\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "plt.rcParams['font.sans-serif']=['SimHei'] \n",
    "#绘制热力图\n",
    "sns.heatmap(cor_mat, linewidths=0.2, vmax=1, vmin=-1, linecolor='w',\n",
    "            annot=True,annot_kws={'size':8},square=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方差扩大因子(variance inflation factor)简称VIF  >10 有多重共线性\n",
    "from statsmodels.stats.outliers_influence import variance_inflation_factor\n",
    "l1 =list(map(lambda x: variance_inflation_factor(data_cor.values, x),range(data_cor.shape[1]))) \n",
    "l1\n",
    "# 在使用账户数量 存在多重共线性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.drop(\"在使用账户数量\",axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_cor = data_cor.iloc[:,[0,2]]\n",
    "from statsmodels.stats.outliers_influence import variance_inflation_factor\n",
    "l1 =list(map(lambda x: variance_inflation_factor(data_cor.values, x),range(data_cor.shape[1]))) \n",
    "l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stats.ttest_ind(data.在使用可循环贷款账户数量[(~data.在使用可循环贷款账户数量.isna()) & (data.是否违约Y ==1)].astype(int),data.在使用可循环贷款账户数量[(~data.在使用可循环贷款账户数量.isna()) & (data.是否违约Y ==0)].astype(int))\n",
    "# 无显著作用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.drop([\"在使用可循环贷款账户数量\"],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.缺失值处理10分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#查看是否有缺失\n",
    "#方法1\n",
    "data.info()\n",
    "#方法2\n",
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.apply(lambda x: x.isna().sum()/x.size,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.dropna(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.账户数量[data.账户数量<0] = 0 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.衍生字段每个4分（至少5个共20分）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "违约： 1. 还款意愿 ， 刻画（逾期次数，最大逾期天数，平均逾期天数,违约次数，芝麻分数，征信）\n",
    "       2. 还款能力，  贷款收入比，平均收入，收入离散系数，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#从还款能力和还款意愿两个方面出发\n",
    "#车龄\n",
    "#贷款收入比\n",
    "data[\"贷款收入比\"] = data.贷款金额/data[\"月均收入(元)\"]\n",
    "data.贷款收入比[data.贷款收入比 == np.inf]  =0 \n",
    "data.贷款收入比[data.贷款收入比.isna()] =0 \n",
    "#贷款金额与售价比\n",
    "data[\"贷款金额与售价比\"] = data.贷款金额/data[\"汽车购买金额(元)\"]\n",
    "data.贷款金额与售价比[data.贷款金额与售价比 == np.inf]  =0 \n",
    "data.贷款金额与售价比[data.贷款金额与售价比.isna()] =0 \n",
    "#分期付款的首次交款占售价比比重\n",
    "data[\"分期付款的首次交款占售价比比重\"] = data.分期付款的首次交款/data[\"汽车购买金额(元)\"]\n",
    "data.分期付款的首次交款占售价比比重[data.分期付款的首次交款占售价比比重 == np.inf]  =0 \n",
    "data.分期付款的首次交款占售价比比重[data.分期付款的首次交款占售价比比重.isna()] =0 \n",
    "# 存款贷款\n",
    "# 存款收入\n",
    "# 收入稳定性（月收入的离散系数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.是否曾经破产.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.是否曾经破产 = np.where(data.是否曾经破产=='N',0,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 失衡数据判断并处理 10分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4648\n",
       "1    1197\n",
       "Name: 是否违约Y, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.是否违约Y.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.17005988023952096"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "994/data.是否违约Y.size\n",
    "#接近20% 不算失衡数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6.多个备选模型比较20分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "# 模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 集成学习和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier\n",
    "import xgboost as xgb\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "# 评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score, classification_report\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.drop([\"样本权重\"],inplace=True,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in data.columns:\n",
    "    if data[i].dtype == np.float64:\n",
    "        data[i] = data[i].astype(\"float32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(data.iloc[:,1:],data.是否违约Y,test_size=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(X_train, y_train, X_test, y_test, model,model_name):\n",
    "    \n",
    "    print('训练{}'.format(model_name))\n",
    "    \n",
    "    #创建指定模型\n",
    "    clf=model \n",
    "    start = time.time()\n",
    "    \n",
    "    #训练模型\n",
    "    clf.fit(X_train, y_train.values.ravel())\n",
    "    \n",
    "    #验证模型\n",
    "    print(\"训练集评估\")\n",
    "    train_pre = clf.predict(X_train) \n",
    "    print(classification_report(y_train,train_pre))\n",
    "    \n",
    "    print(\"检验集评估\")\n",
    "    test_pre=clf.predict(X_test)\n",
    "    print(classification_report(y_test,test_pre))\n",
    "\n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "\n",
    "    return clf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name_param_dict = { 'LR':(LogisticRegression()),\n",
    "                          'DT': (DecisionTreeClassifier()),\n",
    "                          'AdaBoost': (AdaBoostClassifier()),\n",
    "                          'RF': (RandomForestClassifier()),\n",
    "                          'XGBoost':(XGBClassifier())\n",
    "                         }\n",
    "result = {}\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    result[model_name] = train_model(X_train, y_train, X_test, y_test, model,model_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 7.利用网格搜索调优20分\n",
    "#RF 模型最好\n",
    "param_grid = {'n_estimators': [20, 50, 100,300],\"max_depth\":[4,6,8,10,12],\n",
    "             \"criterion\": [\"gini\", \"entropy\"],\"max_features\": [10,15,20,5],},\n",
    "model = RandomForestClassifier()\n",
    "grid_search = GridSearchCV(model, param_grid, cv=3, scoring='roc_auc')\n",
    "temp=grid_search.fit(X_train, y_train)\n",
    "temp.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = RandomForestClassifier(n_estimators =,max_depth=,criterion= ,max_features=)\n",
    "train_model(X_train, y_train, X_test, y_test, model,\"随机森林\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 8.优质模型保存5分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "数据处理的保存：\n",
    "制造商：编码——独热(10) —— 10列    进了模型      1000 * 30 数据  建模\n",
    "\n",
    "\n",
    "5条记录\n",
    "制造商：编码——独热(5)  —— 5列    进入模型预测  会不会出错？  5 * 25 数据 预测\n",
    "\n",
    "\n",
    "训练模型时\n",
    "制造商： 1——10 自然编号  c -1  d-2  e-3\n",
    "\n",
    "制造商\n",
    "C\n",
    "D\n",
    "e\n",
    "\n",
    "\n",
    "标准化   \n",
    "训练    0-1    10000-100\n",
    " 8000     （ 8000-100）/（10000-100） = 0.82\n",
    "\n",
    "预测数据（5）\n",
    "测试    0-1     8000-10\n",
    " 8000  =     1\n",
    "\n",
    "\n",
    "#优质模型保存\n",
    "\n",
    "from sklearn.externals import joblib\n",
    "#保存模型\n",
    "joblib.dump(temp,'model.pkl')\n",
    "\n",
    "#加载模型\n",
    "#clf=joblib.load('model.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
